﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using VanTaiBLL;
using VanTaiData;
using VanTaiProperty;
using Roles;
using System.Web.UI.HtmlControls;

namespace VanTaiTMS.SubPage
{
    public partial class uc_noi_giao_hang : System.Web.UI.UserControl
    {
        #region[Define]
        private DataTable dtDelivery;
        protected string SuggestionList;
        protected string StrAlert = "";
        private clsTMDeliveryBusiness clsDelivery = new clsTMDeliveryBusiness();
        #endregion

        #region[Controller]
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        //---------------button-popup---------------------
        protected void imgrefesh_Click(object sender, ImageClickEventArgs e)
        {

        }
        protected void imgSave_Click(object sender, ImageClickEventArgs e)
        {
            if (txtMaNoiGiaoHang.Text == "")
            {
                lbThongBaoLoi.Text = "Vui lòng điền vào mã nơi giao hàng !";
                OpenDialog("#ucthongbaonoigiaohang");
                updatePanelThongBao.Update();
            }
            if (txtTenNoiGiaoHang.Text == "")
            {
                lbThongBaoLoi.Text = "Vui lòng điền vào tên nơi giao hàng !";
                OpenDialog("#ucthongbaonoigiaohang");
                updatePanelThongBao.Update();
            }
            if (txtMaNoiGiaoHang.Text != "" && txtTenNoiGiaoHang.Text != "")
            {
                InsertDelivery();
            }
        }
        protected void cmdCloseThongBao_Click(object sender, EventArgs e)
        {
            CloseDialog("#ucthongbaonoigiaohang");
        }
        protected void ImgRefeshDuyet_Click(object sender, ImageClickEventArgs e)
        {

        }
        protected void ImgRefeshExport_Click(object sender, ImageClickEventArgs e)
        {
            //txtTuNgayNhapKhoExport.Text = "";
            //txtDenNgayNhapKhoExport.Text = "";
            //BindGridLenhDieuDongExportExcel();
        }
        #endregion

        #region[Function]
        protected bool CheckInsert()
        {
            bool kiemtra = false;
            dtDelivery = clsDelivery.BindGirdTMDelivery();
            if (dtDelivery != null && dtDelivery.Rows.Count > 0)
            {
                foreach (DataRow dr in dtDelivery.Rows)
                {
                    if (txtMaNoiGiaoHang.Text == dr["Delivery_Alias"].ToString())
                    {
                        kiemtra = true;
                    }
                }
            }
            return kiemtra;
        }
        protected bool CheckInsertNameDelivery()
        {
            bool kiemtra = false;
            dtDelivery = clsDelivery.BindGirdTMDelivery();
            if (dtDelivery != null && dtDelivery.Rows.Count > 0)
            {
                foreach (DataRow dr in dtDelivery.Rows)
                {
                    if (txtTenNoiGiaoHang.Text == dr["Delivery_Name"].ToString())
                    {
                        kiemtra = true;
                    }
                }
            }
            return kiemtra;
        }
        protected void InsertDelivery()
        {
            try
            {
                if (CheckInsert())
                {
                    lbThongBaoLoi.Text = "Mã nơi giao hàng này đã tồn tại !";
                    OpenDialog("#ucthongbaonoigiaohang");
                    updatePanelThongBao.Update();
                }
                else
                {
                    if (CheckInsertNameDelivery())
                    {
                        lbThongBaoLoi.Text = "Tên nơi giao này đã tồn tại !";
                        OpenDialog("#ucthongbaonoigiaohang");
                        updatePanelThongBao.Update();
                    }
                    else
                    {
                        VanTaiProperty.clsTMDeliveryProperty obj = new clsTMDeliveryProperty();
                        obj.Delivery_Alias = txtMaNoiGiaoHang.Text;
                        obj.Delivery_Name = txtTenNoiGiaoHang.Text;
                        obj.Delivery_Note = txtGhiChu.Text;
                        VanTaiBLL.clsTMDeliveryBusiness.InsertTMDelivery(obj);
                        lbThongBaoLoi.Text = "Thêm mẫu tin thành công !";
                        OpenDialog("#ucthongbaonoigiaohang");
                        updatePanelThongBao.Update();
                        ClearTextBox();
                    }
                }
            }
            catch (Exception ex)
            {
                lbThongBaoLoi.Text = ex.ToString();
                OpenDialog("#ucthongbaonoigiaohang");
                updatePanelThongBao.Update();
            }
        }
        #endregion

        #region[OpenDialog]
        private void OpenDialog(string dialogId)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.Append("$('" + dialogId + "').dialog('open');");
            ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(),
               "scriptKey", sb.ToString(), true);
        }
        private void CloseDialog(string dialogId)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.Append("$('" + dialogId + "').dialog('close');");
            ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(),
               "scriptKey", sb.ToString(), true);
        }
        #endregion

        #region[Clear-TextBox]
        protected void ClearTextBox()
        {
            txtMaNoiGiaoHang.Text = "";
            txtTenNoiGiaoHang.Text = "";
            txtGhiChu.Text = "";
        }
        #endregion
    }
}